package com.partsoft.dits.log;

import java.util.Date;
import java.util.List;

import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

import com.partsoft.dits.EdgeDescriptor;

public abstract class AbstractLogResolver implements LogResolver {
	
	public int getLogCount() {
		return resolveLogCount(null, null, null, null);
	}
	
	public int getLogCount(LogLevel logLevel) {
		return resolveLogCount(null, null, logLevel, null);
	}

	public int getLogCount(EdgeDescriptor descriptor, LogLevel logLevel) {
		return resolveLogCount(descriptor, null, logLevel, null);
	}

	public int getLogCount(EdgeDescriptor descriptor) {
		return resolveLogCount(descriptor,  null, null, null);
	}

	public List<LogEntity> getLogs() {
		return resolveLogs(null, null, null, null, 0, -1);
	}

	public List<LogEntity> getLogs(LogLevel logLevel) {
		return resolveLogs(null, null, logLevel, null, 0, -1);
	}

	public List<LogEntity> getLogs(int from, int count) {
		Assert.isTrue(from>=0);
		return resolveLogs(null, null, null, null, from, count);
	}

	public List<LogEntity> getLogs(LogLevel logLevel, int from, int count) {
		Assert.isTrue(from >= 0);
		return resolveLogs(null, null, logLevel, null, from, count);
	}

	public List<LogEntity> getLogs(EdgeDescriptor descriptor) {
		return resolveLogs(descriptor, null, null, null, 0, -1);
	}

	public List<LogEntity> getLogs(EdgeDescriptor descriptor, LogLevel logLevel) {
		return resolveLogs(descriptor, null, logLevel, null, 0, -1);
	}
	
	public List<LogEntity> getLogs(EdgeDescriptor descriptor, LogType logType) {
		return resolveLogs(descriptor, logType, null, null, 0, -1);
	}

	public List<LogEntity> getLogs(EdgeDescriptor descriptor, int from, int count) {
		Assert.isTrue(from >= 0);
		return resolveLogs(descriptor, null, null, null, from, count);
	}
	
	public List<LogEntity> getLogs(EdgeDescriptor descriptor, String queryString, int from, int count) {
		Assert.isTrue(from >= 0);
		return resolveLogs(descriptor, null, null, queryString, from, count);
	}
	
	public List<LogEntity> getLogs(EdgeDescriptor descriptor, LogLevel logLevel, int from, int count) {
		Assert.isTrue(from >= 0);
		return resolveLogs(descriptor, null, logLevel, null, from, count);
	}
	
	public List<LogEntity> getLogs(EdgeDescriptor descriptor, LogType logType, int from, int count) {
		Assert.isTrue(from >= 0);
		return resolveLogs(descriptor, logType, null, null, from, count);
	}
	
	public List<LogEntity> getLogs(EdgeDescriptor descriptor, LogType logType, LogLevel logLevel, String queryString, int from, int count) {
		Assert.isTrue(from >= 0);
		return resolveLogs(descriptor, logType, logLevel, queryString, from, count);
	}
	
	public int getLogCount(String queryString) {
		return resolveLogCount(null, null, null, queryString);
	}

	public List<LogEntity> getLogs(String queryString) {
		return resolveLogs(null, null, null, queryString, 0, -1);
	}
	
	public int getLogCount(LogLevel logLevel, String queryString) {
		return resolveLogCount(null, null, logLevel, queryString);
	}
	
	public List<LogEntity> getLogs(LogLevel logLevel, String queryString) {
		return resolveLogs(null, null, logLevel, queryString, 0, -1);
	}

	public int getLogCount(EdgeDescriptor descriptor, LogType logType) {
		return resolveLogCount(descriptor, logType, null, null);
	}
	
	public int getLogCount(EdgeDescriptor descriptor, String queryString) {
		return resolveLogCount(descriptor, null, null, queryString);
	}
	
	public List<LogEntity> getLogs(EdgeDescriptor descriptor, String queryString) {
		return resolveLogs(descriptor, null, null, queryString, 0, -1);
	}
	
	public int getLogCount(LogType logType) {
		return resolveLogCount(null, logType, null, null);
	}
	public List<LogEntity> getLogs(LogType logType) {
		return resolveLogs(null, logType, null, null, 0, -1);
	}
	
	public int getLogCount(LogType logType, LogLevel logLevel, String queryString) {
		return resolveLogCount(null, logType, logLevel, queryString);
	}
	
	public List<LogEntity> getLogs(LogType logType, LogLevel logLevel, String queryString) {
		return resolveLogs(null, logType, logLevel, queryString, 0, -1);
	}
	
	public int getLogCount(EdgeDescriptor descriptor, LogType logType, LogLevel logLevel, String queryString) {
		return resolveLogCount(descriptor, logType, logLevel, queryString);
	}
	
	public List<LogEntity> getLogs(EdgeDescriptor descriptor, LogType logType, LogLevel logLevel, String queryString) {
		return resolveLogs(descriptor, logType, logLevel, queryString, 0, -1);
	}
	
	public List<LogEntity> getLogs(String queryString, int from, int count) {
		Assert.isTrue(from >= 0);
		return resolveLogs(null, null, null, queryString, from, count);
	}
	
	public List<LogEntity> getLogs(LogLevel logLevel, String queryString, int from, int count) {
		Assert.isTrue(from >= 0);
		return resolveLogs(null, null, logLevel, queryString, from, count);
	}
	

	public int getLogCount(EdgeDescriptor descriptor, LogType logType, LogLevel logLevel, String queryString, Date beginDay, Date endDay) {
		return resolveLogCount(descriptor, logType, logLevel, queryString, beginDay, endDay);
	}

	public List<LogEntity> getLogs(EdgeDescriptor descriptor, LogType logType, LogLevel logLevel, String queryString, Date beginDay, Date endDay, int from, int count) {
		return resolveLogs(descriptor, logType, logLevel, queryString, beginDay, endDay, from, count);
	}

	/**
	 * 获得日志条目
	 * @param descriptor 边界描述符，如果为null则表示不分边界 
	 * @param logLevel 日志类型，如果为null则表示不分日志类型
	 * @param from 开始计数，如果为-1，则表示返回所有日志
	 * @param count 总数
	 */
	protected List<LogEntity> resolveLogs(EdgeDescriptor descriptor, LogType logType, LogLevel logLevel, String queryString, Date beginDay, Date endDay, int from, int count) {
		String dclsName = null;
		String did = null;
		if (descriptor != null) {
			dclsName = ClassUtils.getUserClass(descriptor).getName();
			did = descriptor.getId();
		}
		return resolveLogs(dclsName, did, logType, logLevel, queryString, beginDay, endDay, from, count);
	}
	
	protected abstract List<LogEntity> resolveLogs(String dclsName, String did, LogType logType, LogLevel logLevel, String queryString, Date beginDay, Date endDay, int from, int count);
	
	protected List<LogEntity> resolveLogs(EdgeDescriptor descriptor, LogType logType, LogLevel logLevel, String queryString, int from, int count) {
		return resolveLogs(descriptor, logType, logLevel, queryString, null, null, from, count);
	}
	
	
	protected int resolveLogCount(EdgeDescriptor descriptor, LogType logType, LogLevel logLevel, String queryString) {
		return resolveLogCount(descriptor, logType, logLevel, queryString, null, null);
	}
	
	/**
	 * 获得日志条数
	 * @param descriptor 如果为null，则表示不分边界
	 * @param logLevel 如果为null则表示不分类型
	 */
	protected int resolveLogCount(EdgeDescriptor descriptor, LogType logType, LogLevel logLevel, String queryString, Date beginDay, Date endDay) {
		String dclsName = null;
		String did = null;
		if (descriptor != null) {
			dclsName = ClassUtils.getUserClass(descriptor).getName();
			did = descriptor.getId();
		}
		return resolveLogCount(dclsName, did, logType, logLevel, queryString, beginDay, endDay);
	}
	
	abstract protected int resolveLogCount(String dclsName, String did, LogType logType, LogLevel logLevel, String queryString, Date beginDay, Date endDay);

	public int getLogCount(String dclsName, String did, LogType logType, LogLevel logLevel, String queryString, Date beginDay, Date endDay) {
		return resolveLogCount(dclsName, did, logType, logLevel, queryString, beginDay, endDay);
	}

	public List<LogEntity> getLogs(String dclsName, String did, LogType logType, LogLevel logLevel, String queryString, Date beginDay, Date endDay, int from, int count) {
		return resolveLogs(dclsName, did, logType, logLevel, queryString, beginDay, endDay, from, count);
	}
	
	
	
	
	
}
